<?php

namespace app\controllers;

use yii\web\Controller;
use app\models\Customer;
use app\models\Order;

class TestController extends Controller
{
    public function actionIndex()
    {
        // 关联查询结果缓存
        //$customer = Customer::find()->where(['name' => 'zhangsan'])->one();
        //$orders = $customer->orders; // select * from order where customer_id = ...
        //unset($customer->orders);
        //$orders2 = $customer->orders; // select * from order where customer_id = ...

        // 关联查询的多次查询
        //$customers = Customer::find()->all(); // select * from customer 假如有100个顾客
        //foreach ($customers as $customer) { // 循环100次，共101次SQL查询
        //    $orders = $customer->orders; // select * from order where customer_id = ...
        //}

        // select * from customer
        // select * from order where customer_id in (...)
        $customers = Customer::find()->with('orders')->all(); // 假如有100个顾客
        foreach ($customers as $customer) { // 此时不再执行SQL操作，因此由101次SQL查询简化为2次SQL查询
            $orders = $customer->orders;
        }
    }
    /*
    public function actionIndex()
    {
        // 关联查询

        // 根据顾客查询其订单信息
        //$customer = Customer::find()->where(['name' => 'zhangsan'])->one();
        //$orders = $customer->hasMany('app\models\Order', ['customer_id' => 'id'])->asArray()->all();
        //$orders = $customer->hasMany(Order::className(), ['customer_id' => 'id'])->asArray()->all();
        //$orders = $customer->getOrders();
        //$orders = $customer->orders;
        //print_r($orders);

        //根据订单获取顾客信息
        $order = Order::find()->where(['id' => 3])->one();
        $customer = $order->customer;
        print_r($customer);
    }
    */
    /*
    public function actionIndex()
    {
        // 修改数据
        $test = Test::find()->where(['id' => 4])->one();
        $test->title = 'title4';
        $test->save();
    }
    */
    /*
    public function actionIndex()
    {
        // 添加数据
        $test = new Test();
        $test->id = 3;
        $test->title = 'title3';
        $test->validate();
        if ($test->hasErrors()) {
            echo 'data is error!';
            die;
        }
        $test->save();
    }
    */
    /*
    public function actionIndex()
    {
        // 删除数据
        //$result = Test::find()->where(['id' => 1])->all();
        //$result[0]->delete();

        Test::deleteAll('id>:id', array(':id' => 0));
    }
    */
    /*
    public function actionIndex()
    {
        //$id = '1 or 1=1';
        //$sql = 'SELECT * FROM test WHERE id = :id ';
        //$result = Test::findBySql($sql, array(':id' => 1))->all();

        // id = 1
        //$result = Test::find()->where(['id' => 1])->all();

        // id > 0
        //$result = Test::find()->where(['>', 'id', 0])->all();

        // id between 1 2
        //$result = Test::find()->where(['between', 'id', 1, 2])->all();

        // title like '%title1%'
        //$result = Test::find()->where(['like', 'title', 'title1'])->all();

        // 查询结果转化成数组
        //$result = Test::find()->where(['like', 'title', 'title'])->asArray()->all();

        //print_r($result);

        // 批量查询
        foreach (Test::find()->batch(1) as $tests) { // 每次取两条数据
            print_r($tests);
        }
    }
    */
}